#include "mainwindow.h"
#include "homepage.h"
#include "page/common_page/setting/settingspage.h"
#include "page/user_page/personal/personal.h"
#include "page/user_page/material/materialaccess.h"
#include "page/manager_page/log/logdetail.h"
#include "page/manager_page/material/materialdetail.h"
#include "page/manager_page/user_manage/usermanagement.h"
#include "page/manager_page/warehouse_permission/warehousepermission.h"
#include <QMenuBar>
#include <QMenu>
#include <QAction>
#include <QPalette>


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    setWindowTitle("菜单页面示例");
    setGeometry(150, 120, 1600, 800);

    // 初始化页面容器
    stackedWidget = new QStackedWidget(this);
    setCentralWidget(stackedWidget);

    // 创建各个页面
    homePage = new HomePage(this);
    settingsPage = new SettingsPage(this);

    personal = new Personal(this);
    materialAccess = new MaterialAccess(this);

    logDetail = new LogDetail(this);
    materialDetail = new MaterialDetail(this);
    userManagement = new UserManagement(this);
    warehousePermission = new WarehousePermission(this);

    // 将页面添加到容器中
    stackedWidget->addWidget(homePage);
    stackedWidget->addWidget(settingsPage);

    stackedWidget->addWidget(personal);
    stackedWidget->addWidget(materialAccess);

    stackedWidget->addWidget(logDetail);
    stackedWidget->addWidget(materialDetail);
    stackedWidget->addWidget(userManagement);
    stackedWidget->addWidget(warehousePermission);
    // 创建菜单栏
    createMenusUser();

    // 1. 重置所有菜单的默认样式（关键：避免样式叠加）
    QString style = "QMenuBar { background-color: #f0f0f0; }"
                    "QMenuBar::item { padding: 5px 15px; background-color: #f0f0f0; }"  // 默认背景
                    "QMenuBar::item:selected { background-color: #b3d700; }";  // 悬停效果

    menuBar()->setStyleSheet(style);
}

MainWindow::~MainWindow()
{
    // Qt的父对象机制会自动销毁子部件，不需要手动delete
}

void MainWindow::createMenusUser()
{
    // 创建"首页"菜单和动作
    QMenu *homeMenu = menuBar()->addMenu("首页");
    QAction *homeAction = homeMenu->addAction("首页1");
    connect(homeAction, &QAction::triggered, this, &MainWindow::showHomePage);
    // 创建"设置"菜单和动作
    QMenu *settingsMenu = menuBar()->addMenu("设置");
    QAction *settingsAction = settingsMenu->addAction("系统设置");
    connect(settingsAction, &QAction::triggered, this, &MainWindow::showSettingsPage);


    // 创建"物料操作"菜单和动作
    QMenu *materialAccessMenu = menuBar()->addMenu("物料操作");
    QAction *materialAccessAction = materialAccessMenu->addAction("物料存取");
    connect(materialAccessAction, &QAction::triggered, this, &MainWindow::showMaterialAccess);
    // 创建"个人信息"菜单和动作
    QMenu *personalMenu = menuBar()->addMenu("个人信息");
    QAction *personalAction = personalMenu->addAction("个人信息");
    connect(personalAction, &QAction::triggered, this, &MainWindow::showPersonal);


    // 创建"日志管理"菜单和动作
    QMenu *logDetailMenu = menuBar()->addMenu("日志管理");
    QAction *LogDetailAction = logDetailMenu->addAction("操作日志明细");
    connect(LogDetailAction, &QAction::triggered, this, &MainWindow::showLogDetail);
    // 创建"物料管理"菜单和动作
    QMenu *materialDetailMenu = menuBar()->addMenu("物料管理");
    QAction *materialDetailAction = materialDetailMenu->addAction("物料明细");
    connect(materialDetailAction, &QAction::triggered, this, &MainWindow::showMaterialManagement);
    // 创建"人员管理"菜单和动作
    QMenu *userManagementMenu = menuBar()->addMenu("人员管理");
    QAction *userManagementAction = userManagementMenu->addAction("人员信息");
    connect(userManagementAction, &QAction::triggered, this, &MainWindow::showUserManagement);
    // 创建"仓库管理"菜单和动作
    QMenu *warehousePermissionMenu = menuBar()->addMenu("仓库管理");
    QAction *warehousePermissionAction = warehousePermissionMenu->addAction("仓库权限定义");
    connect(warehousePermissionAction, &QAction::triggered, this, &MainWindow::showWarehousePermission);

}


void MainWindow::createMenusManager()
{

}

void MainWindow::slotLoginSuccess()
{

}

/*
 *公用页面
 */
void MainWindow::showHomePage()
{
    stackedWidget->setCurrentWidget(homePage);
}
void MainWindow::showSettingsPage()
{
    stackedWidget->setCurrentWidget(settingsPage);
}

/*
 * 用户相关页面
 */
void MainWindow::showPersonal()
{
    stackedWidget->setCurrentWidget(personal);
}
void MainWindow::showMaterialAccess()
{
    stackedWidget->setCurrentWidget(materialAccess);
}

/*
 * 管理员相关页面
 */
void MainWindow::showMaterialManagement()
{
    stackedWidget->setCurrentWidget(logDetail);
}
void MainWindow::showUserManagement()
{
    stackedWidget->setCurrentWidget(materialDetail);
}
void MainWindow::showLogDetail()
{
    stackedWidget->setCurrentWidget(userManagement);
}
void MainWindow::showWarehousePermission()
{
    stackedWidget->setCurrentWidget(warehousePermission);
}
